package androidx.lifecycle;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Iterator;
import java.util.Map.Entry;
import o.dc;
import o.dh;
import o.kp.c;
import o.kp.e;
import o.ks;
import o.kt;
import o.kv;
import o.kz;

public abstract class LiveData<T> {
    static final Object ˏ = new Object();
    private int ʻ = -1;
    private final Runnable ʼ = new Runnable(this) {
        final /* synthetic */ LiveData ॱ;

        {
            this.ॱ = r1;
        }

        public void run() {
            Object obj;
            synchronized (this.ॱ.ˊ) {
                obj = this.ॱ.ˎ;
                this.ॱ.ˎ = LiveData.ˏ;
            }
            this.ॱ.ॱ(obj);
        }
    };
    private boolean ʽ;
    final Object ˊ = new Object();
    private dh<kz<? super T>, b> ˋ = new dh();
    volatile Object ˎ = ˏ;
    int ॱ = 0;
    private volatile Object ॱॱ = ˏ;
    private boolean ᐝ;

    abstract class b {
        final /* synthetic */ LiveData ʻ;
        final kz<? super T> ˊ;
        int ˋ = -1;
        boolean ॱ;

        abstract boolean ˏ();

        b(LiveData liveData, kz<? super T> kzVar) {
            this.ʻ = liveData;
            this.ˊ = kzVar;
        }

        boolean ˋ(kv kvVar) {
            return false;
        }

        void ˊ() {
        }

        void ˋ(boolean z) {
            int i = 1;
            if (z != this.ॱ) {
                this.ॱ = z;
                int i2 = this.ʻ.ॱ == 0 ? 1 : 0;
                LiveData liveData = this.ʻ;
                int i3 = liveData.ॱ;
                if (!this.ॱ) {
                    i = -1;
                }
                liveData.ॱ = i + i3;
                if (i2 != 0 && this.ॱ) {
                    this.ʻ.ˏ();
                }
                if (this.ʻ.ॱ == 0 && !this.ॱ) {
                    this.ʻ.ॱ();
                }
                if (this.ॱ) {
                    this.ʻ.ˊ(this);
                }
            }
        }
    }

    class LifecycleBoundObserver extends b implements ks {
        final /* synthetic */ LiveData ˎ;
        @NonNull
        final kv ˏ;

        LifecycleBoundObserver(LiveData liveData, @NonNull kv kvVar, kz<? super T> kzVar) {
            this.ˎ = liveData;
            super(liveData, kzVar);
            this.ˏ = kvVar;
        }

        boolean ˏ() {
            return this.ˏ.ac_().ˋ().ˏ(c.STARTED);
        }

        public void ˊ(kv kvVar, e eVar) {
            if (this.ˏ.ac_().ˋ() == c.DESTROYED) {
                this.ˎ.ˎ(this.ˊ);
            } else {
                ˋ(ˏ());
            }
        }

        boolean ˋ(kv kvVar) {
            return this.ˏ == kvVar;
        }

        void ˊ() {
            this.ˏ.ac_().ˋ(this);
        }
    }

    private void ॱ(b bVar) {
        if (!bVar.ॱ) {
            return;
        }
        if (!bVar.ˏ()) {
            bVar.ˋ(false);
        } else if (bVar.ˋ < this.ʻ) {
            bVar.ˋ = this.ʻ;
            bVar.ˊ.ॱ(this.ॱॱ);
        }
    }

    void ˊ(@Nullable b bVar) {
        if (this.ʽ) {
            this.ᐝ = true;
            return;
        }
        this.ʽ = true;
        do {
            this.ᐝ = false;
            if (bVar == null) {
                Iterator ˊ = this.ˋ.ˊ();
                while (ˊ.hasNext()) {
                    ॱ((b) ((Entry) ˊ.next()).getValue());
                    if (this.ᐝ) {
                        break;
                    }
                }
            }
            ॱ((b) bVar);
            bVar = null;
        } while (this.ᐝ);
        this.ʽ = false;
    }

    @MainThread
    public void ॱ(@NonNull kv kvVar, @NonNull kz<? super T> kzVar) {
        ॱ("observe");
        if (kvVar.ac_().ˋ() != c.DESTROYED) {
            kt lifecycleBoundObserver = new LifecycleBoundObserver(this, kvVar, kzVar);
            b bVar = (b) this.ˋ.ˏ(kzVar, lifecycleBoundObserver);
            if (bVar != null && !bVar.ˋ(kvVar)) {
                throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
            } else if (bVar == null) {
                kvVar.ac_().ॱ(lifecycleBoundObserver);
            }
        }
    }

    @MainThread
    public void ˎ(@NonNull kz<? super T> kzVar) {
        ॱ("removeObserver");
        b bVar = (b) this.ˋ.ˊ(kzVar);
        if (bVar != null) {
            bVar.ˊ();
            bVar.ˋ(false);
        }
    }

    @MainThread
    protected void ॱ(T t) {
        ॱ("setValue");
        this.ʻ++;
        this.ॱॱ = t;
        ˊ(null);
    }

    @Nullable
    public T ˊ() {
        T t = this.ॱॱ;
        return t != ˏ ? t : null;
    }

    protected void ˏ() {
    }

    protected void ॱ() {
    }

    public boolean ˋ() {
        return this.ॱ > 0;
    }

    private static void ॱ(String str) {
        if (!dc.ॱ().ˋ()) {
            throw new IllegalStateException("Cannot invoke " + str + " on a background" + " thread");
        }
    }
}
